package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpExprService exprService;

    @Override
    public PageResult<Emp> pageEmp(EmpQueryParam param) {
        Page<Emp> empPage = Page.of(param.getPage(), param.getPageSize());
        empPage.addOrder(OrderItem.desc("update_time"));
        //使用lambdaQuery方法构建查询条件
        lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(empPage);
        //通过设置好的Page对象获取返回值结果
        List<Emp> empList = empPage.getRecords();
        //通过stream流式编程获取到返回值结果中的每一个deptId
        List<Integer> deptIds = empList.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());
        if (!deptIds.isEmpty()){
            Map<Integer, String> collect = deptService.listByIds(deptIds).stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
            empList.forEach(emp -> {
                emp.setDeptName(collect.getOrDefault(emp.getDeptId(),"无"));
            });
        }
        return new PageResult<>(empPage.getTotal(), empList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if(!exprList.isEmpty()){
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        }
        exprService.saveBatch(exprList);
    }

    @Override
    public Emp getByIdEmp(Integer id) {
        Emp emp = getById(id);
        //获取员工经历
        List<EmpExpr> empExprList = exprService.lambdaQuery().eq(EmpExpr::getEmpId, id).list();
        emp.setExprList(empExprList);
        return emp;
    }

    @Override
    public List<Emp> listEmp() {
        List<Emp> empList = list();
        return empList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmp(List<Integer> ids) {
        removeByIds(ids);
        exprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));

    }

}
